package cn.hyatt.auth.service.impl;

import cn.hyatt.auth.entity.SystemRoleMenu;
import cn.hyatt.auth.mapper.SystemRoleMenuMapper;
import cn.hyatt.auth.service.SystemRoleMenuService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 角色-服务实现
 *
 * @author hyatt
 */
@Slf4j
@Service
@Primary
@AllArgsConstructor
public class SystemRoleMenuServiceImpl extends ServiceImpl<SystemRoleMenuMapper, SystemRoleMenu> implements SystemRoleMenuService {
    @Override
    @Transactional
    public void saveRoleMenus(Long roleId, List<Long> menuIds) {
        List<SystemRoleMenu> list = menuIds.stream().map(menuId -> {
            SystemRoleMenu roleMenu = new SystemRoleMenu();
            roleMenu.setRoleId(roleId);
            roleMenu.setMenuId(menuId);
            return roleMenu;
        }).toList();
        saveBatch(list);
    }

    @Override
    @Transactional
    public void deleteRoleMenus(Long roleId) {
        remove(new LambdaQueryWrapper<SystemRoleMenu>()
                .eq(SystemRoleMenu::getRoleId,roleId)
        );
    }
}
